package cn.com.ege.mvc.action.business;

import java.io.Serializable;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.com.ege.mvc.controller.BaseAction;
import cn.com.ege.mvc.controller.IAction;
import cn.com.ege.mvc.exception.BusinessException;
import cn.com.ege.mvc.model.business.Purchase;
import cn.com.ege.mvc.model.view.PurchaseDetailView;
import cn.com.ege.mvc.service.impl.business.PurchaseService;
import cn.com.ege.mvc.util.PageController;

public class PurchaseAction extends BaseAction implements IAction, Serializable {
	private static final long serialVersionUID = 8299975587235537983L;
	private final static Log logger = LogFactory.getLog(OrderListAction.class);
	private PageController pager = null;
	private PurchaseService purchaseService = null;
	private Purchase purchase = null;

	public String execute(HttpServletRequest request,
			HttpServletResponse response, ServletConfig servletConfig) {
		String act = request.getParameter("act");
		if (null != act) {
			// 取列表
			if ("list".equals(act)) {
				int totalRows = purchaseService.getRecordCount(new Purchase());
				String URL = request.getRequestURI();
				this.pager.setURL(URL);
				this.pager.setTotalRowsAmount(totalRows);
				List<Purchase> resourceList = purchaseService.list(
						new Purchase(), this.pager.getPageStartRow(), pager
								.getPageSize(), "order by id desc", true);
				pager.setData(resourceList);
				return getReturnURL(LIST);
			}
			// 点了添加或者点了修改
			else if ("get".equals(act)) {
				purchase = purchaseService.get(purchase);
				return getReturnURL(EDIT);
			}
			// 保存表单
			else if ("save".equals(act)) {
				try {
					purchaseService.save(purchase);
					return getReturnURL("getList");
				} catch (BusinessException e) {
					// 保存原来表单已输入的内容
					request.setAttribute("purchase", purchase);
					addMessage(e.getMessage());
					return getReturnURL(EDIT);
				}
			}
			// 删除
			else if ("del".equals(act)) {
				String[] ids = request.getParameterValues("id");
				for (String id : ids) {
					if (null != id && !("".equals(id))) {
						Purchase purchase = new Purchase();
						purchase.setId(Long.parseLong(id));
						(new PurchaseService()).delete(purchase);
						purchase.setId(Long.parseLong(id));
						purchaseService.delete(purchase);
					}
				}
				return getReturnURL("getList");
			}
		}
		addMessage("参数不正确!! act=" + act);
		return getReturnURL(ERROR);
	}

	public PageController getPager() {
		return pager;
	}

	public void setPager(PageController pager) {
		this.pager = pager;
	}

	public PurchaseService getPurchaseService() {
		return purchaseService;
	}

	public void setPurchaseService(PurchaseService purchaseService) {
		this.purchaseService = purchaseService;
	}

	public Purchase getPurchase() {
		return purchase;
	}

	public void setPurchase(Purchase purchase) {
		this.purchase = purchase;
	}

}
